<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
pre_common_task 论坛任务表

taskid, smallint, 任务id,
relatedtaskid, smallint, 依存任务idn必须完成依存任务才能申请该任务,
available, tinyint, 是否启用,
name, varchar, 任务名称,
description, text, 任务描述,
icon, varchar, 任务图标,
applicants, mediumint, 已申请任务人次,
achievers, mediumint, 已完成任务人次,
tasklimits, mediumint, 允许申请并完成该任务的人次上限,
applyperm, text, 允许申请任务的用户组id, 格式:	1	2	3,
scriptname, varchar, 任务脚本文件名,
starttime, int, 任务上线时间,
endtime, int, 任务下线时间,
period, int, 任务周期 单位：小时 默认为0表示一次性任务 设置为24即1天表示日常任务,
periodtype, tinyint, 任务间隔周期单位 0:小时 1:天 2:周 3:月,
reward, enum, 奖励类型n(credit:积分 magic:道具 medal:勋章 invite:邀请码 group:特殊用户组),
prize, varchar, 奖品: 哪一个扩展积分, 道具id, 勋章id,邀请码有效期， 特殊用户组id,
bonus, int, 奖品数量/有效期: n积分数量, 道具数量, 勋章有效期, 邀请码数量，特殊用户组有效期,
displayorder, smallint, 显示顺序,
version, varchar, 任务脚本版本号,

 *      $Id: table_common_task.php 27777 2012-02-14 07:07:26Z zhengqingpeng $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_common_task extends discuz_table
{
	public function __construct() {

		$this->_table = 'common_task';
		$this->_pk    = 'taskid';

		parent::__construct();
	}

	public function fetch_all_by_available($available) {
		return DB::fetch_all("SELECT * FROM %t WHERE available=%d", array($this->_table, $available), $this->_pk);
	}

	public function fetch_all_data() {
		return DB::fetch_all("SELECT * FROM %t ORDER BY displayorder, taskid DESC", array($this->_table));
	}

	public function count_by_scriptname($scriptname) {
		return DB::result_first("SELECT COUNT(*) FROM %t WHERE scriptname=%s", array($this->_table, $scriptname));
	}

	public function fetch_all_by_scriptname($scriptname) {
		return DB::fetch_all("SELECT * FROM %t WHERE scriptname=%s", array($this->_table, $scriptname));
	}

	public function update_by_scriptname($scriptname, $data) {
		if(!$data || !is_array($data)) {
			return;
		}
		DB::update($this->_table, $data, DB::field('scriptname', $scriptname));
	}

	public function update_applicants($taskid, $v) {
		DB::query("UPDATE %t SET applicants=applicants+%s WHERE taskid=%d", array($this->_table, $v, $taskid));
	}

	public function update_achievers($taskid, $v) {
		return DB::query("UPDATE %t SET achievers=achievers+%s WHERE taskid=%d", array($this->_table, $v, $taskid));
	}

	public function update_available() {
		DB::query("UPDATE %t SET available='2' WHERE available='1' AND starttime>'0' AND starttime<=%d AND (endtime IS NULL OR endtime>%d)", array($this->_table, TIMESTAMP, TIMESTAMP), false, true);
	}

	public function fetch_all_by_status($uid, $status) {
		switch($status) {
			case 'doing':
				$status = "mt.status='0'";
				break;
			case 'done':
				$status = "mt.status='1'";
				break;
			case 'failed':
				$status = "mt.status='-1'";
				break;
			case 'canapply':
			case 'new':
			default:
				$status = "'".TIMESTAMP."' > starttime AND (endtime=0 OR endtime>'".TIMESTAMP."') AND (mt.taskid IS NULL OR (ABS(mt.status)='1' AND t.period>0))";
				break;
		}
		return DB::fetch_all("SELECT t.*, mt.csc, mt.dateline FROM %t t
			LEFT JOIN %t mt ON mt.taskid=t.taskid AND mt.uid=%d
			WHERE %i AND t.available='2' ORDER BY t.displayorder, t.taskid DESC", array($this->_table, 'common_mytask', $uid, $status));
	}

	public function fetch_by_uid($uid, $taskid) {
		return DB::fetch_first("SELECT t.*, mt.dateline, mt.dateline AS applytime, mt.status, mt.csc FROM %t t LEFT JOIN %t mt ON mt.uid=%d AND mt.taskid=t.taskid
			WHERE t.taskid=%d AND t.available='2'", array($this->_table, 'common_mytask', $uid, $taskid));
	}

}

?>